import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject";
import { Subscription } from "rxjs/Subscription";
import { SheetDataType, ShowDataType, BUYINRATE, SALEOUTRATE } from "./../vtradeterminal.model";
import { VtradeTerminalService } from "./../vtradeterminal.service";
import { HsClientService } from "./../../hs-client.service";

@Component({
  selector: 'vtrade-modal',
  templateUrl: 'vtrademodal.component.html',
  styleUrls: ['vtrademodal.component.less']
})
export class VtradeModalComponent implements OnInit, OnDestroy {
  // 委托交易的数据[账户、代码、名称、价格、数量] account, stock, action, type
  @Output() onHide = new EventEmitter();
  @Output() onConfirm = new EventEmitter();
  tradeTitle: string;
  isSell = false;
  symbol: string;
  amount: string;
  price: string;
  allPrice: string;
  charge: string;
  KEYLIST = ['symbol', 'name', 'price', 'amount'];

  constructor(private vts: VtradeTerminalService, private hsClient: HsClientService) { }
  dataList = [{
    label: '交易账户',
    key: 'account',
    value: ''
  }, {
    label: '股票代码',
    key: 'symbol',
    value: ''
  }, {
    label: '股票名称',
    key: 'name',
    value: ''
  }, {
    label: '委托价格',
    key: 'price',
    value: ''
  }, {
    label: '委托数量',
    key: 'amount',
    value: ''
  }, {
    label: '所属市场',
    key: 'market',
    value: '',
    isHide: true
  }];
  sheetList = [];
  tempObj = {
    symbol: '',
    name: '',
    price: '',
    amount: '',
    market: ''
  };
  buyinObj: ShowDataType = Object.assign(this.tempObj);
  saleoutObj: ShowDataType = Object.assign(this.tempObj);
  account: string;
  hsTokenSubscription: Subscription;
  sheetDataSubscription: Subscription;

  ngOnInit() {
    this.hsClient.getTokenSource.connect();
    // 恒生token
    this.hsTokenSubscription = this.hsClient.getTokenSource.subscribe(res => {
      this.dataList[0].value = res.account;
      this.account = res.account;
    });

    this.vts.sheetDataSource.connect();
    // 交易数据表单
    this.sheetDataSubscription = this.vts.sheetDataSource.subscribe((data: SheetDataType) => {
      console.log('%c[vtrade-modal] data:', 'color:#f09', data);
      switch (data.type) {
        case "buyin":
          this.isSell = false;
          this.tradeTitle = '买入委托';
          this.buyinObj = data.buyin;
          this.sheetList = this.getShowList(this.dataList, data.buyin);
          break;
        case "saleout":
          this.isSell = true;
          this.tradeTitle = '卖出委托';
          this.saleoutObj = data.saleout;
          this.sheetList = this.getShowList(this.dataList, data.saleout);
          break;
      }
    });

    // enter事件，快速交易
    Observable.fromEvent(window, "keydown")
      .debounceTime(300)
      .subscribe((e: KeyboardEvent) => {
        if (e.key === "Enter") {
          if (this.isSell) {
            this.onConfirmClick("saleout");
          } else {
            this.onConfirmClick("buyin");
          }
        }
      });
  }

  ngOnDestroy() {
    this.hsTokenSubscription ? this.hsTokenSubscription.unsubscribe() : null;
    this.sheetDataSubscription ? this.sheetDataSubscription.unsubscribe() : null;
  }

  getShowList(targetList, originData) {
    let tempList = [...targetList];
    tempList.map(item => {
      switch (item.key) {
        case "price":
          item.value = originData[item.key] + '元';
          break;
        case "amount":
        case "symbol":
        case "name":
          item.value = originData[item.key];
          break;
        case "market":
          item.value = this.transform(originData[item.key]);
          break;
        case "account":
          break;
      }
    });
    return tempList;
  }

  // 转换市场信息
  transform(value: string): string {
    let retString = "";
    switch (value) {
      case "XSHG.ESA.M":
        retString = "上证A股";
        break;
      case "XSHG.ESB":
        retString = "上证B股";
        break;
      case "XSHE.ESA.M":
        retString = "深证A股";
        break;
      case "XSHE.ESB":
        retString = "深证B股";
        break;
      case "XSHE.ESA.SMSE":
        retString = "中小板";
        break;
      case "XSHE.ESA.GEM":
        retString = "创业板";
        break;
      case "XSHG.MRI":
      case "XSHE.MRI":
        retString = "沪深指数";
        break;
      case "XBHS.GN":
        retString = "概念板块";
        break;
      case "XBHS.HY":
        retString = "行业板块";
        break;
      case "XBHS.DY":
        retString = "地域板块";
        break;
      case "XBHS.ZJHHY":
        retString = "证监会行业";
        break;
    }
    return retString;
  }

  cancel() {
    this.onHide.next('hide');
  };

  // 确认交易
  onConfirmClick(type: string) {
    let dataObj = {
      type: type,
      buyin: this.buyinObj,
      saleout: this.saleoutObj
    };
    this.onConfirm.next(dataObj);
  }
}